//
//  TTEmoticonSource.swift
//  TTCOM-macOS
//
//  Created by Toj on 3/29/21.
//

import Foundation
import HandyJSON

open class TTEmoticonSource: NSObject {
    
    private(set) var emoticon: [TTEmoticon]?
    public var name = ""
    public var ext: String?
    
    public init(source name: String, ext: String?) {
        self.name = name
        self.ext = ext
      
        _ = BundleResource.shared.url(source: name, ext: ext)
    }
    
    public func path(resource name: String, ofType: String?) -> [TTEmoticon]? {
        let bundle = BundleResource.shared.url(source: self.name, ext: self.ext)
        guard let path = bundle?.path(forResource: name, ofType: ofType) else { return nil }
        var sourcies = [TTEmoticon]()
       
        [TTEmoticon].deserialize(from: NSArray(contentsOfFile: path))?.forEach({ (emoji) in
            guard let emoji = emoji else { return }
            let imagePath = bundle?.path(forResource:emoji.image, ofType: nil) ?? ""
            let largeImagePath = bundle?.path(forResource:emoji.largeImage, ofType: nil) ?? ""
            emoji.imagePath = imagePath
            print(imagePath)
            emoji.largeImagePath = largeImagePath
            sourcies.append(emoji)
        })

        emoticon = sourcies
        return sourcies
    }
}
